# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 15:34:56 2012

@author: Timothy.Wiseman

This works as pure python code, now start playing with Cython. 

"""

class PicBox(object):
    #made to work with PIL, so returns a sizeTuple that works
    #with functions such as Image.resize()
    
    __slots__ = ('le up ri lo')
    defaultFrac = 1
    defaultOffset = 0
    
    def __init__(self, le, up, ri, lo):
        self.le = le
        self.up = up
        self.ri = ri
        self.lo = lo

    @property
    def width(self):
        #PIL users coordate system with 0, 0 in upper lsef
        return abs(self.ri - self.le)
    
    @property
    def height(self):
        #PIL uses coordinate system with 0, 0 in upper left
        return abs(self.lo-self.up)
        
    @property
    def size(self):
        return self.width *self.height
        
    @property
    def sizeTuple(self):
        return (self.width, self.height)
        
    #These functions return quadrants of the box    
    #all division is ineger division
    def ulBox(self):
        """Returns the box occupying the upper 1/9th of the current box"""
        return PicBox(
                    self.le, 
                    self.up, 
                    self.le + self.width//3, 
                    self.up + self.height//3
                    )
                           
    def urBox(self):
        return PicBox(
                self.le + self.width*2//3,
                self.up,
                self.ri,
                self.up + self.height//3)
                
    def lrBox(self):
        return PicBox(
                self.le + self.width*2//3,
                self.up +self.height*2//3,
                self.ri,
                self.lo)
                
    def llBox(self):
        return PicBox(
                self.le,
                self.up +self.height*2//3,
                self.le + self.width//3,
                self.lo)
                
    def centerBox(self):
        """The box occupying the center 1/9th the current box"""
        return PicBox(
                    self.le + self.width//3, #left
                    self.up + self.height//3, #up
                    self.le + self.width*2//3, #right
                    self.up + self.height*2//3, #lower
                    )
                
    #Ok, there is a better way to hanle the offset dir, need to refactor all but the last of these
                
    def urCornorBox(self, frac = defaultFrac, offset = defaultOffset, offsetDir = 1):
        """Returns the box coming from the upper right cornor of the current
        box with the specified fraction of the size.  This may become negative
        or it may exceed the size of a given area under consideration.
        
        It is moved down by the percent specified in offset.  To get the 
        exact cornor, use an offset of 0.        
        
        offsetDir 1 is clockwise, offset dir -1 is counterclockwise.
        """

        newle = self.ri #left
        newri = self.ri + int(self.width * frac) #right
        
        #offset part
        if offsetDir == 1:
            newup = self.up - int(self.height * frac)  + int(self.height * offset) #upper
            newlo = self.up        + int(self.height * offset) #lower
            newle = self.ri #left
            newri = self.ri + int(self.width * frac) #right
        else:
            newup = self.up - int(self.height * frac) #upper
            newlo = self.up  #lower
            newle = self.ri  - int(self.width * offset)#left
            newri = self.ri + int(self.width * frac)  - int(self.width * offset) #right

        return PicBox(newle, newup, newri, newlo)
                
    def ulCornorBox(self, frac = defaultFrac, offset = defaultOffset, offsetDir = 1):
        """Returns the box coming from the upper left cornor of the current
        box with the specified fraction of the size.  This may become negative
        or it may exceed the size of a given area under consideration.
        
        Shifted right by the offset.  To get the exact cornor, use an offset 
        of 0."""
        #nonoffset

        
        if offsetDir == 1:
            newle = self.le - int(self.width * frac)    + int(self.width*offset) #left  
            newri = self.le       + int(self.width*offset) #right
            newup =  self.up - int(self.height * frac) #upper 
            newlo = self.up #lower
        else:
            newle = self.le - int(self.width * frac) #left
            newri = self.le #right
            newup =  self.up - int(self.height * frac) + int(self.height * offset)#upper 
            newlo = self.up + int(self.height * offset)#lower            
        
        return PicBox(newle, newup, newri, newlo)
        
            
    def llCornorBox(self, frac = defaultFrac, offset = defaultOffset, offsetDir = 1):
        """Returns the box coming from the lower left cornor of the current
        box with the specified fraction of the size.  This may become negative
        or it may exceed the size of a given area under consideration.
        
        Shifted up by the offset.  to get the exact cornor, use an offset of 0."""

        
        if offsetDir == 1:
            newup = self.lo    -int(self.height * offset) #upper
            newlo = self.lo + int(self.height * frac)     -int(self.height * offset) #lower
            newle = self.le- int(self.width * frac) #left
            newri = self.le #right
        else:
            newup = self.lo     #upper
            newlo = self.lo + int(self.height * frac)    #lower   
            newle = self.le- int(self.width * frac)  + int(self.width*offset)#left
            newri = self.le  + int(self.width*offset)#right
        
        return PicBox(newle, newup, newri, newlo)
                
    def lrCornorBox(self, frac = defaultFrac, offset = defaultOffset, offsetDir = 1):
        """Returns the box coming from the lower right cornor of the current
        box with the specified fraction of the size.  This may become negative
        or it may exceed the size of a given area under consideration.
        
        It is moved left by the percent specified in offset.  To get the 
        exact cornor, use an offset of 0"""
        
        if offsetDir == 1:
            newle = self.ri    - offsetDir * int(self.width * offset) #left
            newup = self.lo #upper
            newri = self.ri + int(self.width *frac)     - offsetDir * int(self.width * offset) #right
            newlo = self.lo + int(self.height * frac) #lower
        else:
            newle = self.ri     #left
            newup = self.lo-int(self.height * offset) #upper
            newri = self.ri + int(self.width *frac)     #right
            newlo = self.lo + int(self.height * frac) -int(self.height * offset)#lower
        
        return PicBox(newle, newup, newri, newlo)